package com.springdemo.annother;

import com.springdemo.Util.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UrlTest {

  public static void main(String[] args) {
    //
      ArrayList<String> cityList = new ArrayList();
      cityList.add("郑州");
      cityList.add("深圳");
      String query="郑州";
      String url="https://quhao.51240.com/"+ URLEncoder.encode(query)+"__quhao/";
      List<Map<String, String>> cityInfoList = cityList.stream().map(city -> getQuhao(city)).collect(Collectors.toList());
      //遍历显示
    cityInfoList.forEach(
        cityInfo -> {
          String info = "";
          for (Map.Entry<String, String> stringStringEntry : cityInfo.entrySet()) {
            //
            info = info + "["+stringStringEntry.getKey()+":" + stringStringEntry.getValue()+"],";
          }
          System.out.println(info);
        });
  }
  public static Map<String,String> getQuhao(String city){
      Map<String,String> map = new HashMap();
      map.put("city",city);
      try {
          String url="https://quhao.51240.com/"+ URLEncoder.encode(city)+"__quhao/";
          System.out.println(url);
          String s = HttpClientUtil.doGet(url, "utf-8");
          Document document = Jsoup.parse(s);
          Element table = document.getElementsByTag("table").last();
          Elements td = table.getElementsByTag("td");
          String quhao=td.get(1).childNode(0).toString();
          String guishudi=(td.get(3).getAllElements().html());
          map.put("quhao",quhao);
          map.put("guishudi",guishudi);
          return map;
      } catch (Exception e) {
          e.printStackTrace();
      }
      map.put("quhao","异常");
      map.put("guishudi","异常");
      return map;
  }

}
